<template>
	<div class="big">

		<div>
			<el-card class="box-card" style="margin: 0 auto;">
				<h2 style="padding-left: 55px;position: relative;" class="el-icon-search">管理员登录</h2>
				<el-form :model="admin" status-icon ref="ruleForm" label-width="100px" :rules="rules">
					<el-form-item label="账号" prop="adminName">
						<el-input type="text" v-model="admin.adminName" placeholder="请输入账号" suffix-icon="el-icon-user">
						</el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-input type="password" v-model="admin.password" placeholder="请输入密码"
							suffix-icon="el-icon-umbrella"></el-input>
					</el-form-item>

					<el-form-item>
						<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
						<el-button @click="resetForm('ruleForm')">重置</el-button>
						<router-link  to="/youx">邮箱验证登录</router-link>
					</el-form-item>
				</el-form>

			</el-card>

		</div>


	</div>


</template>

<script>
	import qs from 'qs'
	export default {
		data() {
			return {
				admin: {
					adminName: 'root',
					password: '123456'
				},
				rules: {
					adminName: [{
							required: true,
							message: '请输入账号',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 5,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 8,
							message: '长度在 3 到 8 个字符',
							trigger: 'blur'
						}
					]
				}
			}
		},
		methods: {
			//登录
			submitForm(formName) {
				this.$refs[formName].validate(async (valid) => {
					if (valid) {


						var res = await this.$http.post('/adminLogin', qs.stringify(this.admin))
						console.log(res.data)
						if (res.data.code == 200) {
							this.$message({

								message: '恭喜你，登录成功',
								type: 'success'
							});
							//将登录成功的token保存到本地
							window.sessionStorage.setItem("token", res.data.token)
							console.log(res.data.token)
							// alert(res.data.token)
							//用户信息
							window.sessionStorage.setItem("user", JSON.stringify(res.data.data))
							window.sessionStorage.setItem("user1", res.data.data.adminName)
							// encodeURIComponent()
							window.sessionStorage.setItem("adminId", res.data.data.adminId)
							//跳转界面
							this.$router.push('/AdminHome')
						} else {
							this.$message.error('用户名或账号不合法');
						}

					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			//重置
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style>
	.big {

		padding-top: 12.5rem;
		margin: 0 auto;
		width: 25rem;
		height: 37.5rem;
	}
</style>
